#include <iostream>


using namespace std;

const int N = 1e6 + 10;
int ne[N], e[N];
int h, id;
int map[N]; 

int main()
{
	int q;
	cin >> q;

	id++;
	e[id] = 1;
	map[1] = id;

	while (q--)
	{
		int cur, x, y;
		cin >> cur >> x;
		int index = map[x];
		if (cur == 1) // 插入
		{
            cin >> y;
			id++;
			e[id] = y;
			ne[id] = ne[index];
			ne[index] = id;
			map[y] = id;
		}
		else if(cur == 2) // 查询
		{
			cout << e[ne[index]] << endl;
		}
		else
		{
			ne[index] = ne[ne[index]];
		}
	}
	return 0;
}
